package com.wangku.springmvc.entity;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

@SuppressWarnings("rawtypes")
public class PageData extends HashMap implements Map {

	private static final long serialVersionUID = 1L;

	Map map = null;
	HttpServletRequest request;

	@SuppressWarnings("unchecked")
	public PageData(HttpServletRequest request) {
		this.request = request;
		Map properties = request.getParameterMap();
		Map returnMap = new HashMap();
		Iterator entries = properties.entrySet().iterator();
		Map.Entry entry;
		String name = "";
		String value = "";
		while (entries.hasNext()) {
			entry = (Map.Entry) entries.next();
			name = (String) entry.getKey();
			Object valueObj = entry.getValue();
			if (null == valueObj) {
				value = "";
			} else if (valueObj instanceof String[]) {
				String[] values = (String[]) valueObj;
				value = "";
				for (int i = 0; i < values.length; i++) {
					value += values[i] + ",";
				}
				value = value.substring(0, value.length() - 1);
			} else {
				value = valueObj.toString();
			}
			returnMap.put(name, value);
		}
		map = returnMap;
	}

	public PageData() {
		map = new HashMap();
	}

	@Override
	public Object get(Object key) {
		Object obj = null;
		if (map.get(key) instanceof Object[]) {
			Object[] arr = (Object[]) map.get(key);
			obj = request == null ? arr : (request.getParameter((String) key) == null ? arr : arr[0]);
		} else {
			obj = map.get(key);
		}
		return obj;
	}

	public String getString(Object key) {
		Object value = get(key);
		return value == null ? "" : (String) value;
	}
	
	public int getInt(Object key) {
		int i = 0;
		Object object = get(key);
		if (object != null && !"".equals(object)) {
			if (object instanceof Integer) {
				i = (Integer) object;
			} else {
				i = Integer.parseInt(object.toString());
			}
		}
		return i;
	}

	public long getLong(Object key) {
		long i = 0;
		Object object = get(key);
		if (object != null && !"".equals(object)) {
			if (object instanceof Long) {
				i = (Long) object;
			} else if (object instanceof String) {
				i = Long.parseLong(object.toString());
			}
		}
		return i;
	}

	public double getDouble(Object key) {
		double i = 0;
		Object object = get(key);
		if (object != null && !"".equals(object)) {
			if (object instanceof Double) {
				i = (Double) object;
			} else {
				i = Double.parseDouble(object.toString());
			}
		}
		return i;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object put(Object key, Object value) {
		return map.put(key, value);
	}

	@Override
	public Object remove(Object key) {
		return map.remove(key);
	}

	public void clear() {
		map.clear();
	}

	public boolean containsKey(Object key) {
		return map.containsKey(key);
	}

	public boolean containsValue(Object value) {
		return map.containsValue(value);
	}

	public Set entrySet() {
		return map.entrySet();
	}

	public boolean isEmpty() {
		return map.isEmpty();
	}

	public Set keySet() {
		return map.keySet();
	}

	@SuppressWarnings("unchecked")
	public void putAll(Map t) {
		map.putAll(t);
	}

	public int size() {
		return map.size();
	}

	public Collection values() {
		return map.values();
	}

	
	public String getString(String param, String defaultValue) {
		if (param == null || param.trim().isEmpty()) {
			return defaultValue;
		}
		
		Object value = map.get(param);
		return value != null ? value.toString().trim() : defaultValue;		
	}
	
	public Integer getInt(String param, Integer defaultValue) {
		if (map == null || map.isEmpty() || param == null) {
			return defaultValue;
		}

		Object value = map.get(param);
		if (value == null) {
			return defaultValue;
		}
		try {
			return Integer.parseInt(value.toString().trim());
		} catch (NumberFormatException e) {
			return defaultValue;
		}
	}

	public Long getLong(String param, Long defaultValue) {
		if (map == null || map.isEmpty() || param == null) {
			return defaultValue;
		}

		Object value = map.get(param);
		if (value == null) {
			return defaultValue;
		}
		try {
			return Long.parseLong(value.toString().trim());
		} catch (NumberFormatException e) {
			return defaultValue;
		}
	}

	public Double getDouble(String param, Double defaultValue) {
		if (map == null || map.isEmpty() || param == null) {
			return defaultValue;
		}

		Object value = map.get(param);
		if (value == null) {
			return defaultValue;
		}
		try {
			return Double.parseDouble(value.toString().trim());
		} catch (NumberFormatException e) {
			return defaultValue;
		}
	}
	
}
